package com.seele.tigerwallet.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.ui.manager.LoadingManager;
import com.seele.tigerwallet.ui.utils.ActionBarTintManager;
import com.seele.tigerwallet.ui.utils.StatusBarUtil;
import com.seele.tigerwallet.ui.utils.StatusBarUtils;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public abstract class BaseActivity extends AppCompatActivity {

    protected LoadingManager mLoadingManager;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        init(savedInstanceState);
        init();

    }

    protected void init(Bundle savedInstanceState) {
    }

    protected abstract int getLayoutId();

    protected abstract void init();


    protected void initWidow() {

       StatusBarUtils.with(this).init();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            StatusBarUtil.setStatusBarLightMode(getWindow());
        }
        if (getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 沉浸式状态栏适配
     * @param window
     * @param dark
     * @return
     */
    public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 沉浸式状态栏适配
     * @param window
     * @param dark
     * @return
     */
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }



    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        onResumed();
    }



    /**
     * 基类的获取焦点方法
     */
    protected void onResumed() {

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        onPaused();
    }

    /**
     * 基类的Paused
     */
    protected void onPaused() {
    }


    protected void setWindowTranslucent() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }


    public void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(this, cls);
        this.startActivity(intent);
    }


    public LoadingManager getLoadingManager() {
        if (null == mLoadingManager) {
            mLoadingManager = new LoadingManager(this);
        }
        return mLoadingManager;
    }


    /**
     * 显示加载图标
     */
    public void showLoadingDialog() {
        getLoadingManager().showLoadingDialog();
    }

    public void dismissLoadingDialog() {
        if (mLoadingManager != null) {
            mLoadingManager.dismissLoadingDialog();
            mLoadingManager = null;
        }
    }

    public void startActivity(Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (null != bundle) {
            intent.putExtras(bundle);
            startActivity(intent);
        }

    }

    public void showAlertDialog(String message, DialogInterface.OnClickListener onClickListener) {
        new AlertDialog.Builder(this)
                .setTitle(getString(R.string.alert_tips))
                .setMessage(message)
                .setCancelable(false)
                .setPositiveButton(getString(R.string.alert_ok), onClickListener)
                .create().show();
    }

    public void showAlertDialog(int resId, DialogInterface.OnClickListener onClickListener) {
        new AlertDialog.Builder(this)
                .setTitle(getString(R.string.alert_tips))
                .setMessage(getString(resId))
                .setCancelable(false)
                .setPositiveButton(getString(R.string.alert_ok), onClickListener)
                .create().show();
    }



}
